package cn.com.common.tools;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class FileUtil {
	/**
	 * test logs
	 * @param inputstr
	 * @param encodingtype
	 * @throws IOException
	 */
	public static void Logs(String inputstr, String encodingtype)
			throws IOException {
		String baseuri = System.getProperty("user.dir") + "\\logs";
		File file = new File(baseuri);
		if (!file.exists()) {
			file.mkdir();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String datestr = sdf.format(new Date());
		String filename = baseuri + "\\" + datestr + ".log";
		if (encodingtype == null || "".equals(encodingtype)) {
			encodingtype = "utf-8";
		}
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(filename, true), encodingtype));
		SimpleDateFormat sdffile = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String datefilestr = sdffile.format(new Date());
		bw.write(datefilestr + "----->" + inputstr);
		bw.newLine();
		bw.close();
	}

	/**
	 * file store
	 * 
	 * @param inputstream
	 * @param desturl
	 * @return
	 * @throws IOException
	 */
	public static String Storefile(InputStream inputstream, String desturl)
			throws IOException {
		if (inputstream == null) {
			return "false";
		}
		BufferedInputStream bisread = new BufferedInputStream(inputstream);
		if (bisread.available() <= 0) {
			return "false";
		}
		if (desturl == null || "".equals(desturl)) {
			String baseuri = System.getProperty("user.dir") + "\\postfile";
			File file = new File(baseuri);
			if (!file.exists()) {
				file.mkdir();
			}
		}
		BufferedOutputStream boswrite = new BufferedOutputStream(
				new FileOutputStream(desturl));
		int seglength = 0;
		byte[] inputbytes = new byte[1024];
		while ((seglength = bisread.read(inputbytes)) != -1) {

			boswrite.write(inputbytes, 0, seglength);
		}
		bisread.close();
		boswrite.close();
		return "ok";
	}

	/**
	 * image store
	 * @param image
	 * @param response
	 */
	public static void writeImage(byte[] image, HttpServletResponse response) {
		if (image == null) {
			return;
		}
		byte[] buffer = new byte[1024];
		InputStream is = null;
		OutputStream os = null;
		try {
			is = new ByteArrayInputStream(image);
			os = response.getOutputStream();
			while (is.read(buffer) != -1) {
				os.write(buffer);
				os.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
				if (os != null) {
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @param filePath
	 * @param response
	 */
	public static void writeImage(String filePath, HttpServletResponse response) {
		File imageFile = new File(filePath);
		if (imageFile != null && imageFile.exists()) {
			byte[] buffer = new byte[1024];
			InputStream is = null;
			OutputStream os = null;
			try {
				is = new FileInputStream(imageFile);
				os = response.getOutputStream();
				while (is.read(buffer) != -1) {
					os.write(buffer);
					os.flush();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (is != null) {
						is.close();
					}
					if (os != null) {
						os.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * @param file
	 * @param savePath
	 * @return
	 */
	public static String uploadToLocal(CommonsMultipartFile file,
			String savePath) {
		if (file == null || file.isEmpty()) {
			return "false";
		}
		String fileName = file.getOriginalFilename();
		String suffixName = fileName.substring(fileName.indexOf(".") + 1);
		String newFileName = System.currentTimeMillis() + "." + suffixName;
		File fileDir = new File(savePath);
		if (!fileDir.exists()) {
			fileDir.mkdirs();
		}
		String filePath = savePath + newFileName;
		FileOutputStream fos = null;

		try {
			fos = new FileOutputStream(filePath);
			fos.write(file.getBytes());
			fos.flush();
			return "ok";
		} catch (Exception e) {
			e.printStackTrace();
			return "false";
		} finally {
			try {
				if (fos != null) {
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				return "false";
			}
		}
	}
}
